/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package job.manager.client.mb;

import com.icesoft.faces.component.ext.HtmlCommandButton;
import com.icesoft.faces.component.ext.RowSelectorEvent;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.event.ActionEvent;
import job.manager.db.ec.ProfilesEB;
import job.manager.db.sb.ProfileRemote;
import job.manager.icefaces.core.SortableList;

/**
 *
 * @author Robban
 */
public class ProfileMBDT extends SortableList implements Serializable {

    @EJB
    private ProfileRemote profileBean;
    private Object[] proList;
    private ProfilesEB selectedProfile;
    // profileEB Names
    private static final String idColumnName = "Id";
    private static final String userNameColumnName = "fname";
    private static final String emailColumnName = "lname";
    private static final String isAdminColumnName = "number";
    private static final String profileColumnName = "comment";

    /** Creates a new instance of ProfileMBDT */
    public ProfileMBDT() {
        super(idColumnName);
    }

    @PostConstruct
    public void onInit() {
    }

    public void loadProfiles() {
        proList = profileBean.getAllProfiles().toArray();
    }

    public Object[] getProfilelist() {
        if (proList == null) {
            this.loadProfiles();
        }
        if (!oldSort.equals(sortColumnName) ||
                oldAscending != ascending) {
            sort();
            oldSort = sortColumnName;
            oldAscending = ascending;
        }
        return proList;
    }

    @Override
    protected void sort() {
        Comparator comparator = new Comparator() {

            public int compare(Object o1, Object o2) {
                ProfilesEB p1 = (ProfilesEB) o1;
                ProfilesEB p2 = (ProfilesEB) o2;
                if (sortColumnName == null) {
                    return 0;
                }
                if (sortColumnName.equals("profilid")) {
                    return ascending ? new Integer(p1.getId()).compareTo(new Integer(p2.getId())) : new Integer(p2.getId()).compareTo(new Integer(p1.getId()));
                } else if (sortColumnName.equals("profilfname")) {
                    return ascending ? p1.getName1().compareTo(p2.getName1()) : p2.getName1().compareTo(p1.getName1());
                } else if (sortColumnName.equals("profillname")) {
                    return ascending ? p1.getName2().compareTo(p2.getName2()) : p2.getName2().compareTo(p1.getName2());
                } else if (sortColumnName.equals("profilnumber")) {
                    return ascending ? p1.getNumber().compareTo(p2.getNumber()) : p2.getNumber().compareTo(p1.getNumber());
                } else if (sortColumnName.equals("profilcomment")) {
                    return ascending ? p1.getComment().compareTo(p2.getComment()) : p2.getComment().compareTo(p1.getComment());
                } else {
                    return 0;
                }
            }
        };
        Arrays.sort(proList, comparator);
    }

    @Override
    protected boolean isDefaultAscending(String sortColumn) {
        return true;
    }

    public void deleteAction(ActionEvent arg) {
        if (arg.getComponent() instanceof HtmlCommandButton) {
            if (this.selectedProfile != null) {
                profileBean.deleteProfile(selectedProfile);
                loadProfiles();
            }
        }
    }

    public void updateAction(ActionEvent arg) {
        if (this.selectedProfile != null) {
            profileBean.updateProfile(selectedProfile);
        }
    }

    public boolean getIsEmployee() {
        if (this.selectedProfile != null) {
            return this.selectedProfile.getIsEmployee();
        } else {
            return true;
        }
    }

    public void setIsEmployee(boolean value)
    {
        if (this.selectedProfile != null) {
            this.selectedProfile.setIsEmployee(value);
            this.selectedProfile.setIsCustomer(!value);
        }
        else{
            this.selectedProfile = new ProfilesEB();
            this.selectedProfile.setIsEmployee(value);
            this.selectedProfile.setIsCustomer(!value);
        }
    }

    public ProfilesEB getSelectedProfile() {
        if (this.selectedProfile != null) {
            return this.selectedProfile;
        } else {
            return new ProfilesEB();
        }
    }

    public void selectAction(RowSelectorEvent arg) {
        this.selectedProfile = (ProfilesEB) proList[arg.getRow()];
    }
}
